/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.view.utils;

import ar.com.gruposicom.controller.OperationController;
import ar.com.gruposicom.domain.Command;
import ar.com.gruposicom.domain.Operation;
import ar.com.gruposicom.domain.Politic;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author marcelo
 */
public class UIAddOperationUtils 
{
    private Operation operation;
    private Collection<Command> commands = new ArrayList<>();
    private Collection<Operation> operations = new ArrayList<>();
    private Collection<Politic> politics = new ArrayList<>();

    public UIAddOperationUtils(Operation o) 
    {
        this.operation = o;
    }
    
    

    public Operation getOperation() {
        return operation;
    }

    public void setOperation(Operation operation) {
        this.operation = operation;
    }

    public void addCommand(Command c) 
    {
        this.commands.add(c);
        System.out.println("Cuando se agrego el comando a la operacion la lista de politicas tiene "+c.getPolitics().size()+" elementos");
        this.mostrarPoliticas();
        this.politics = c.getPolitics();
    }
    
    public void loadOperations(JTable table)
    {
        this.clearTable(table);
        OperationController controller = new OperationController();
        operations.clear();
        operations = controller.getAll();
        Iterator<Operation> it = operations.iterator();
        // ahora obtengo el modelo de la tabla
        DefaultTableModel modelo = (DefaultTableModel)table.getModel();
        String datos[] = new String[2];
        while(it.hasNext())
        {
            Operation o = it.next();
            datos[0] = String.valueOf(o.getId());
            datos[1] = o.getName().trim();
            modelo.addRow(datos);
        }
        it = null;
    }
    
    private void clearTable(JTable table)
    {
        DefaultTableModel modelo = (DefaultTableModel)table.getModel();
        while(table.getRowCount() != 0)
                modelo.removeRow(0);
        modelo = null;
    }
    
    public void saveOperation(String name, JTable table)
    {
        this.mostrarPoliticas();
        operation = new Operation();
        operation.setName(name);
        operation.setCommand(commands);
        this.mostrarPoliticas();
        OperationController controller = new OperationController();
        controller.saveOperation(operation);
        commands.clear();
        this.loadOperations(table);
    }
    
    
    private void mostrarPoliticas()
    {
        Iterator<Command> it = this.commands.iterator();
        while(it.hasNext())
        {
            Command c = it.next();
            System.out.println("El comando "+c.getCommand()+" tiene "+c.getPolitics().size()+" elementos");
        }
    }
    
}
